<!-- templates/login.html -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>系统登录</title>
    <link href="https://g.alicdn.com/code/lib/bootstrap/4.5.3/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-6">
                <div class="card mt-5">
                    <div class="card-body">
                        <h2 class="card-title text-center">系统登录</h2>
                        <!-- 显示错误信息 -->
                        {% if error %}
                        <div class="alert alert-danger" role="alert">
                            用户名或密码有误！
                        </div>
                        {% endif %}

                        <form action="/login" method="post">
                            <div class="form-group">
                                <label for="username">用户名:</label>
                                <input type="text" class="form-control" id="username" name="username" required>
                            </div>
                            <div class="form-group">
                                <label for="password">密码:</label>
                                <input type="password" class="form-control" id="password" name="password" required>
                            </div>
                            <button type="submit" class="btn btn-primary btn-block">登录</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="/static/crypto-js.min.js"></script>
    <script>
    document.addEventListener('DOMContentLoaded', (event) => {
        const loginForm = document.querySelector('form');
        loginForm.addEventListener('submit', function(event) {
            event.preventDefault();
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const md5Password = CryptoJS.MD5(password).toString(); // 使用MD5加密密码
            const formData = new FormData();
            formData.append('username', username);
            formData.append('password', md5Password); // 使用加密后的密码
            fetch('/login', {
                method: 'POST',
                body: formData
            }).then(response => {
                if (response.redirected) {
                    window.location.href = response.url;
                }
            }).catch(error => {
                console.error('Error:', error);
            });
        });
    });
    </script>
</body>
</html>
